package com.zhaosc.parse;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

public class ServiceBeanDefinitionParser implements BeanDefinitionParser {
	private Class<?> beanClass;

	public ServiceBeanDefinitionParser(Class<?> beanClass) {
		this.beanClass = beanClass;
	}

	@Override
	public BeanDefinition parse(Element element, ParserContext parserContext) {
		String interf=element.getAttribute("interface");
		String ref=element.getAttribute("ref");
		String protocol=element.getAttribute("protocol");
		
		RootBeanDefinition beanDefinition=new RootBeanDefinition();
		beanDefinition.setBeanClass(beanClass);
		beanDefinition.setLazyInit(false);
		
        if (protocol == null || "".equals(protocol)) {
            throw new RuntimeException("Service protocol 不能为空！");
        }
        if (interf == null || "".equals(interf)) {
            throw new RuntimeException("Service interf 不能为空！");
        }
        
        if (ref == null || "".equals(ref)) {
            throw new RuntimeException("Service ref 不能为空！");
        }
		
		
		beanDefinition.getPropertyValues().add("protocol", protocol);
		beanDefinition.getPropertyValues().add("interf", interf);
		beanDefinition.getPropertyValues().add("ref", ref);
		parserContext.getRegistry().registerBeanDefinition("service"+ref+interf, beanDefinition);
		return beanDefinition;
	}

}
